<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-card class="box-card">
      <div class="query-card">
        <!-- <el-row type="flex" align="middle">
        <el-col :span="12">-->
        <div class="grid-content bg-purple search">
          <el-form ref="query" :model="query" label-width="100px">
            <el-form-item style="margin-bottom:0px" label="操作">
                <i slot="label" class="el-icon-search" style="font-size:20px"></i>
              <el-select
                v-model="query.class_id"
                @focus="getClassification"
                placeholder="请选择操作分类"
                :loading="optionLoading"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-bottom:0px" label="操作日期">
              <el-date-picker
                v-model="query.timeArr"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
        <!-- </el-col> -->
        <!-- <el-col :span="12"> -->
        <div class="grid-content bg-purple-light">
          <el-button type="primary" @click="queryAsk">查询</el-button>
          <el-button @click="queryReset">重置</el-button>
        </div>
        <!-- </el-col>
        </el-row>-->
      </div>
    </el-card>

    <!-- 表格 -->
    <el-card class="box-card">
      <el-table
        :data="tableData"
        v-loading="loading"
        :header-cell-style="{background:'#eee'}"
        border
        style="width: 100%;margin-bottom: 20px;"
      >
        <el-table-column align="center" type="index" width="50" label="序号"></el-table-column>
        <el-table-column align="center" prop="details" show-overflow-tooltip label="操作内容"></el-table-column>
        <el-table-column align="center" prop="admin_name" label="操作人名称"></el-table-column>
        <el-table-column align="center" prop="admin_account" label="操作人账户"></el-table-column>
        <el-table-column align="center" prop="class_name" label="分类"></el-table-column>
        <el-table-column align="center" prop="add_time" label="操作时间"></el-table-column>
      </el-table>
      <div style="text-align:right">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page="currentPage"
          :total="total"
          @current-change="currentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { getOperateList, classification } from "@/api/system";
import { parseTime } from "@/utils/index";

export default {
  name: "Operate",
  data() {
    return {
      // 分页数据
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //query
      query: {
        class_id: "",
        add_time: "",
        timeArr: []
      },
      options: "",
      tableData: [],
      loading: false,
      optionLoading: false
    };
  },
  mounted() {
    this.getOperateList();
  },
  methods: {
    //获取操作列表
    async getOperateList() {
      this.loading = true;
      const res = await getOperateList({
        page: this.currentPage,
        limit: this.pageSize,
        class_id: this.query.class_id,
        add_time: this.query.add_time
      });
      this.loading = false;
      console.log(res);
      this.tableData = res.data;
      this.tableData.forEach(item => {
        if (item.add_time == 0) {
          item.add_time = "-";
        } else {
          item.add_time = parseTime(item.add_time, "{y}-{m}-{d} {h}:{i}:{s}");
        }
      });
      this.total = res.count;
    },
    queryAsk() {
      console.log(this.query);
      this.currentPage = 1;
      this.query.add_time =
        parseTime(this.query.timeArr[0], "{y}-{m}-{d}") +
        "," +
        parseTime(this.query.timeArr[1], "{y}-{m}-{d}");
      this.getOperateList();
    },
    queryReset() {
      this.resetForm("query");
      this.total = 0;
      this.currentPage = 1;
      this.pageSize = 10;
      this.query.add_time = "";
      this.query.class_id = "";
      this.query.timeArr = [];
      this.getOperateList();
    },
    //分页改变
    currentChange(current) {
      this.currentPage = current;
      this.getOperateList();
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取操作分类
    async getClassification() {
      this.optionLoading = true;
      const res = await classification("");
      this.optionLoading = false;
      console.log(res);
      this.options = res.data;
    }
  }
};
</script>

<style lang="scss" scoped>
.query-card {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.box-card {
  margin-bottom: 20px;
}
.search {
  display: flex;
  align-items: center;
}
::v-deep .el-input--medium .el-input__inner {
  width: 360px;
}
::v-deep .el-drawer.rtl {
  width: 650px !important;
  overflow: auto;
}
::v-deep .el-form {
  display: flex;
}
</style>